package com.meichis.mcsappframework.dialog;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

import com.meichis.mcsappframework.R;
import com.meichis.mcsappframework.utils.ScreenUtil;

import java.util.*;

public class DatePickPopupWindow extends PopupWindow {
    private ListView lv_year, lv_month, lv_date;
    private List<Map<String, Object>> yearList, monthList, dateList;
    private SimpleAdapter yearAdapter, monthAdapter, dateAdapter;
    private DatePickedListener mDatePickedListener;
    private Calendar mCalendar;

    public DatePickPopupWindow(Context context, Calendar calendar, DatePickedListener datePickedListener) {
        super();
        mCalendar = calendar;
        View contentView = LayoutInflater.from(context).inflate(R.layout.pop_datapic_window, (ViewGroup) null);
        mDatePickedListener = datePickedListener;
        lv_year = (ListView) contentView.findViewById(R.id.lv_year);
        lv_month = (ListView) contentView.findViewById(R.id.lv_month);
        lv_date = (ListView) contentView.findViewById(R.id.lv_date);
        if (mCalendar == null) {
            mCalendar = Calendar.getInstance();
            mCalendar.setTimeInMillis(System.currentTimeMillis());
        }
        yearList = new ArrayList<>();
        Map<String, Object> yearMap;
        for (int i = 1970; i < 2050; i++) {
            yearMap = new HashMap<>();
            yearMap.put("YEAR", i);
            yearList.add(yearMap);
        }
        monthList = new ArrayList<>();
        Map<String, Object> monthMap;
        for (int i = 1; i < 13; i++) {
            monthMap = new HashMap<>();
            monthMap.put("MONTH", i);
            monthList.add(monthMap);
        }
        dateList = new ArrayList<>();
        Map<String, Object> dateMap;
        for (int i = 1; i < mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH) + 1; i++) {
            dateMap = new HashMap<>();
            dateMap.put("DATE", i);
            dateList.add(dateMap);
        }
        yearAdapter = new SimpleAdapter(context, yearList, R.layout.list_datapic_item, new String[]{"YEAR"}, new int[]{R.id.tv_1}) {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                TextView tv_1 = (TextView) view.findViewById(R.id.tv_1);
                TextView tv_2 = (TextView) view.findViewById(R.id.tv_2);
                (view.findViewById(R.id.tv_select)).setVisibility(View.INVISIBLE);
                (view.findViewById(R.id.iv_right)).setVisibility(View.VISIBLE);
                tv_2.setText(R.string.datapic_year);
                if (tv_1.getText().toString().equals(mCalendar.get(Calendar.YEAR) + "")) {
                    view.setBackgroundColor(0xFFE9E9E9);
                } else {
                    view.setBackgroundColor(0xFFFFFFFF);
                }
                return view;
            }

        };
        monthAdapter = new SimpleAdapter(context, monthList, R.layout.list_datapic_item, new String[]{"MONTH"}, new int[]{R.id.tv_1}) {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                TextView tv_1 = (TextView) view.findViewById(R.id.tv_1);
                TextView tv_2 = (TextView) view.findViewById(R.id.tv_2);
                (view.findViewById(R.id.tv_select)).setVisibility(View.INVISIBLE);
                (view.findViewById(R.id.iv_right)).setVisibility(View.VISIBLE);
                tv_2.setText(R.string.datapic_month);
                if (tv_1.getText().toString().equals((mCalendar.get(Calendar.MONTH) + 1) + "")) {
                    view.setBackgroundColor(0xFFD9D9D9);
                } else {
                    view.setBackgroundColor(0xFFE9E9E9);
                }
                return view;
            }

            //因为月份改完之后，当月的日期天数就会改变，所以在数据变化的时候修改日期天数
            @Override
            public void notifyDataSetChanged() {
                dateList.clear();
                Map<String, Object> dateMap;
                for (int i = 1; i < mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH) + 1; i++) {
                    dateMap = new HashMap<>();
                    dateMap.put("DATE", i);
                    dateList.add(dateMap);
                }
                super.notifyDataSetChanged();
            }

        };
        dateAdapter = new SimpleAdapter(context, dateList, R.layout.list_datapic_item, new String[]{"DATE"}, new int[]{R.id.tv_1}) {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View view = super.getView(position, convertView, parent);
                TextView tv_1 = (TextView) view.findViewById(R.id.tv_1);
                TextView tv_2 = (TextView) view.findViewById(R.id.tv_2);
                (view.findViewById(R.id.iv_right)).setVisibility(View.INVISIBLE);
                tv_2.setText(R.string.datapic_date);
                if (tv_1.getText().toString().equals((mCalendar.get(Calendar.DATE)) + "")) {
                    (view.findViewById(R.id.tv_select)).setVisibility(View.VISIBLE);
                    view.setBackgroundColor(0xFFD9D9D9);
                } else {
                    (view.findViewById(R.id.tv_select)).setVisibility(View.INVISIBLE);
                    view.setBackgroundColor(0xFFD9D9D9);
                }
                return view;
            }

        };
        lv_year.setAdapter(yearAdapter);
        lv_year.setSelection(mCalendar.get(Calendar.YEAR) - 1970 - 2);
        lv_month.setAdapter(monthAdapter);
        lv_month.setSelection(mCalendar.get(Calendar.MONTH) - 5);
        lv_date.setAdapter(dateAdapter);
        lv_date.setSelection(mCalendar.get(Calendar.DATE) - 1 - 2);
        lv_year.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                TextView tv_1 = (TextView) view.findViewById(R.id.tv_1);
                mCalendar.set(Calendar.YEAR, Integer.parseInt(tv_1.getText().toString()));
                yearAdapter.notifyDataSetChanged();
                monthAdapter.notifyDataSetChanged();
                dateAdapter.notifyDataSetChanged();
            }
        });
        lv_month.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                TextView tv_1 = (TextView) view.findViewById(R.id.tv_1);
                mCalendar.set(Calendar.MONTH, (Integer.parseInt(tv_1.getText().toString())) - 1);
                monthAdapter.notifyDataSetChanged();
                dateAdapter.notifyDataSetChanged();
            }
        });
        lv_date.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                TextView tv_1 = (TextView) view.findViewById(R.id.tv_1);
                mCalendar.set(Calendar.DATE, (Integer.parseInt(tv_1.getText().toString())));
                dateAdapter.notifyDataSetChanged();
                mDatePickedListener.picked(mCalendar);
                DatePickPopupWindow.this.dismiss();
            }
        });
        DisplayMetrics display = ScreenUtil.getMetrics();
        setContentView(contentView);
        setWidth(display.widthPixels);//设置popwindow的宽为屏幕的实际像素宽
        setHeight(display.heightPixels * 2 / 5);//设置popwindow的高为屏幕的实际像素宽的2/5
        setFocusable(true);
    }

    public interface DatePickedListener {
        public void picked(Calendar calendar);
    }
}
